\defmodule{UnuranEmpirical}

This class permits one to create generators for empirical and 
quasi-empirical univariate distributions
using UNURAN via its string interface.
The empirical data can be read from a file, from an array, or simply
encoded into the generator specification string.  
When reading from a file or an array, the generator specification 
string must \emph{not} contain a distribution specification string.

\bigskip\hrule

\begin{code}
\begin{hide}
/*
 * Class:        UnuranEmpirical
 * Description:  create generators for empirical distributions using UNURAN
 * Environment:  Java
 * Software:     SSJ 
 * Copyright (C) 2001  Pierre L'Ecuyer and Universite de Montreal
 * Organization: DIRO, Universite de Montreal
 * @author       
 * @since

 * SSJ is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License (GPL) as published by the
 * Free Software Foundation, either version 3 of the License, or
 * any later version.

 * SSJ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * A copy of the GNU General Public License is available at
   <a href="http://www.gnu.org/licenses">GPL licence site</a>.
 */
\end{hide}
package umontreal.iro.lecuyer.randvar;\begin{hide}
import umontreal.iro.lecuyer.probdist.Distribution;
import umontreal.iro.lecuyer.probdist.PiecewiseLinearEmpiricalDist;
import umontreal.iro.lecuyer.rng.RandomStream;\end{hide} 

public class UnuranEmpirical extends RandomVariateGen\begin{hide} {
   private RandUnuran unuran = new RandUnuran();
\end{hide}\end{code}

\subsubsection* {Constructors}
\begin{code}

   public UnuranEmpirical (RandomStream s, String genStr)\begin{hide} {
      if (s == null)
         throw new IllegalArgumentException ("s must not be null.");

      unuran.mainStream = unuran.auxStream = s;
      unuran.init (genStr);
      if (!unuran.isEmpirical()) {
         unuran.close();
         throw new IllegalArgumentException ("not an empirical distribution");
      }
   }\end{hide}
\end{code}
\begin{tabb}
   Constructs a new empirical univariate generator using the specification string
   \texttt{genStr} and stream \texttt{s}.
\end{tabb}
\begin{code}

   public UnuranEmpirical (RandomStream s, RandomStream aux, String genStr)\begin{hide} {
      if (s == null)
         throw new IllegalArgumentException ("s must not be null.");
      if (aux == null)
         throw new IllegalArgumentException ("aux must not be null.");

      unuran.mainStream = s;
      unuran.auxStream = aux;
      unuran.init (genStr);
      if (!unuran.isEmpirical()) {
         unuran.close();
         throw new IllegalArgumentException ("not an empirical distribution");
      }
   }\end{hide}
\end{code}
\begin{tabb}
   Constructs a new empirical univariate generator using the specification string
   \texttt{genStr}, with main stream \texttt{s} and auxiliary stream \texttt{aux}.
\end{tabb}
\begin{code}

   public UnuranEmpirical (RandomStream s,
                           PiecewiseLinearEmpiricalDist dist, String genStr)\begin{hide} {
      if (s == null)
         throw new IllegalArgumentException ("s must not be null.");

      unuran.mainStream = unuran.auxStream = s;
      String gstr = readDistr (dist) + 
        (genStr == null || genStr.equals ("") ? "" : "&" + genStr);
      unuran.init (gstr);
      if (!unuran.isEmpirical()) {
         unuran.close();
         throw new IllegalArgumentException ("not an empirical distribution");
      }
   }\end{hide}
\end{code}
\begin{tabb}  
  Same as \method{UnuranEmpirical}{}\texttt{(s, s, dist, genStr)}.
\end{tabb}
\begin{code}

   public UnuranEmpirical (RandomStream s, RandomStream aux,
                           PiecewiseLinearEmpiricalDist dist, String genStr)\begin{hide} {
      if (s == null)
         throw new IllegalArgumentException ("s must not be null.");
      if (aux == null)
         throw new IllegalArgumentException ("aux must not be null.");

      unuran.mainStream = s;
      unuran.auxStream = aux;
      String gstr = readDistr (dist) + 
        (genStr == null || genStr.equals ("") ? "" : "&" + genStr);
      unuran.init (gstr);
      if (!unuran.isEmpirical()) {
         unuran.close();
         throw new IllegalArgumentException ("not an empirical distribution");
      }
   }\end{hide}
\end{code}
\begin{tabb} 
  Same as \method{UnuranEmpirical}{}\texttt{(s, aux, genStr)}, but reading 
  the observations from the empirical distribution \texttt{dist}. 
  The \texttt{genStr} argument must not contain a distribution part
  because the distribution will be generated from the input stream reader.
\end{tabb}
\begin{code}\begin{hide}
   // Constructs and returns a distribution string for empirical distr.
   private String readDistr (PiecewiseLinearEmpiricalDist dist) {
      StringBuffer sb = new StringBuffer ("distr=cemp; data=(");
      boolean first = true;

      for (int i = 0; i < dist.getN(); i++) {
         if (first)
            first = false;
         else
            sb.append (",");
         sb.append (dist.getObs (i));
      }
      sb.append (")");
      return sb.toString();
   }

\end{hide}\end{code}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
\subsubsection* {Methods}
\begin{code}\begin{hide}

   public double nextDouble() {
      if (unuran.nativeParams == 0)
         throw new IllegalStateException();
      return unuran.getRandCont (unuran.mainStream.nextDouble(), unuran.nativeParams);
   }

   public void nextArrayOfDouble (double[] v, int start, int n) {
      if (v == null || start < 0 || n < 0 || (start+n) > v.length)
         throw new IllegalArgumentException();
      if (unuran.unifArray == null || unuran.unifArray.length < n)
         unuran.unifArray = new double[n];
      if (unuran.mainStream != unuran.auxStream &&
         (unuran.unifAuxArray == null || unuran.unifAuxArray.length < n))
         unuran.unifAuxArray = new double[n];
      unuran.getRandContArray (unuran.nativeParams, unuran.unifArray,
                        unuran.unifAuxArray, v, start, n);
   }

   protected void finalize() {
      unuran.close();
   }

   public Distribution getDistribution() { return null; }
   public RandomStream getStream() { return unuran.mainStream; }\end{hide}

   public RandomStream getAuxStream()\begin{hide} {
      return unuran.auxStream;
   }
}\end{hide}
\end{code}
\begin{tabb}   Returns the auxiliary random number stream.
\end{tabb}
